package com.project.pojo;

/**
 * api返回包装对象
 *
 * @param <T>
 */
public class Response<T> {


    private static final Integer SUCCESS = 0;
    private static final Integer FAIL = 1;

    private Integer code;

    /**
     * 错误消息
     */
    private String msg;

    /**
     * 消息对象
     */
    private T data;

    public Response(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 返回成功
     *
     * @return
     */
    public static <T> Response<T> ok() {
        return new Response<T>(SUCCESS,"操作成功",null);
    }

    /**
     * 返回成功
     *
     * @return
     */
    public static <T> Response<T> ok(String msg) {
        return new Response<T>(SUCCESS,msg,null);
    }

    /**
     * 返回成功
     *
     * @param data
     * @return
     */
    public static <T> Response<T> ok(T data) {
        return new Response<T>(SUCCESS,"操作成功",data);
    }

    /**
     * 返回成功
     *
     * @param data
     * @return
     */
    public static <T> Response<T> ok(String msg,T data) {
        return new Response<T>(SUCCESS,msg,data);
    }


    /**
     * 返回错误
     *
     * @return
     */
    public static <T> Response<T> error() {
        return new Response<T>(FAIL,"操作失败",null);
    }

    /**
     * 返回错误
     *
     * @return
     */
    public static <T> Response<T> error(String msg) {
        return new Response<T>(FAIL,msg,null);
    }

    /**
     * 返回错误
     *
     * @param code
     * @param msg
     * @return
     */
    public static <T> Response<T> error(int code, String msg) {
        return new Response<T>(code,msg,null);
    }


}
